package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.CompanyMapper;
import com.jieshuibao.mapper.InvoiceMapper;
import com.jieshuibao.mapper.PurchaseOrderExMapper;
import com.jieshuibao.mapper.PurchaseOrderMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserExMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.model.db.Company;
import com.jieshuibao.model.db.Invoice;
import com.jieshuibao.model.db.PurchaseOrder;
import com.jieshuibao.model.db.PurchaseOrderExample;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.model.domain.PagePhone;
import com.jieshuibao.util.JsonUtil;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class PurchaseOrderService {
	
	private static final Logger logger = Logger.getLogger(PurchaseOrderService.class);

	@Resource
	private PurchaseOrderMapper purchaseOrderMapper;
	@Resource
	private PurchaseOrderExMapper purchaseOrderExMapper;

	@Resource
	private UserMapper userMapper;
	@Resource
	private CompanyMapper companyMapper;
	@Resource
	private InvoiceMapper invoiceMapper;
	
	@Resource
	private UserDealRecordMapper userDealRecordMapper;
	
	@Resource
	private UserExMapper userExMapper;
	
	@Resource
	private IntegralService integralService;

	public void addPurchaseOrder(PurchaseOrder purchaseOrder) {
		purchaseOrderMapper.insertSelective(purchaseOrder);
	}

	public void delPurchaseOrderByUuid(String uuid) {
		purchaseOrderMapper.deleteByPrimaryKey(uuid);
	}

	public void updatePurchaseOrder(PurchaseOrder purchaseOrder) {
		purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
	}

	public PurchaseOrder getPurchaseOrderByUuid(String uuid) {
		return purchaseOrderMapper.selectByPrimaryKey(uuid);
	}

	public List<PurchaseOrder> getPagePurchaseOrder(
			PurchaseOrderExample purchaseOrderExample) {
		return purchaseOrderMapper.selectPage(purchaseOrderExample);
	}

	public int countPurchaseOrder(PurchaseOrderExample purchaseOrderExample) {
		return purchaseOrderMapper.countByExample(purchaseOrderExample);
	}

	public List<PurchaseOrder> getPurchaseOrdersByPhoneNum(PagePhone pagePhone) {
		return purchaseOrderExMapper.selectPurchasByPhoneNum(pagePhone);
	}

	public void updateUserAndCompanyAndPurchase(PurchaseOrder purchaseOrder,
			User user, Company company) {
		String phoneNumber=null;
		if (user != null) {
			phoneNumber= user.getPhoneNumber();
			userExMapper.updateUserAdvisoryNum(user);
			
		}
		if (purchaseOrder != null) {
			String uuid = purchaseOrder.getUuid();
			Double originalPrice = purchaseOrder.getOriginalPrice();
			double currentPrice = purchaseOrder.getCurrentPrice();
			Date date = new Date();
			//用户增加积分
			if (StringUtils.isNotBlank(phoneNumber)) {
				integralService.integral(phoneNumber, "recharge", (int)currentPrice);
			}
			purchaseOrderMapper.insertSelective(purchaseOrder);
			UserDealRecord userDealRecord1 = new UserDealRecord();
			UserDealRecord userDealRecord2 = new UserDealRecord();
			UserDealRecord userDealRecord3 = new UserDealRecord();
			String uuid1=UUIDGenerator.getUUID();//用户支出
			String uuid2=UUIDGenerator.getUUID();//解税宝公司支出
			String uuid3=UUIDGenerator.getUUID();//用户企业收入
			userDealRecord1.setUuid(uuid1);
			userDealRecord1.setBusinessNo(uuid);
			userDealRecord1.setCreateDate(date);
			userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_VIPCARD);
			userDealRecord1.setPaySucTime(date);
			userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			userDealRecord1.setSum(currentPrice);
			userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
			userDealRecord1.setUserId(phoneNumber);
			userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
			userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
			userDealRecord2.setUuid(uuid2);
			userDealRecord2.setBusinessNo(uuid);
			userDealRecord2.setCreateDate(date);
			userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_VIPCARD);
			userDealRecord2.setPaySucTime(date);
			userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			userDealRecord2.setSum(originalPrice-currentPrice);
			userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
			userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
			userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
			userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYENVELOPES);
			
			
			userDealRecord3.setUuid(uuid3);
			userDealRecord3.setBusinessNo(uuid);
			userDealRecord3.setCreateDate(date);
			userDealRecord3.setGoodsType(PayTypeUtil.GOODSTYPE_VIPCARD);
			userDealRecord3.setPaySucTime(date);
			userDealRecord3.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
			userDealRecord3.setSum(originalPrice);
			userDealRecord3.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
			if(company!=null){
				String uuid4 = company.getUuid();
				userDealRecord3.setUserId(uuid4);
				userDealRecord3.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
			}else {
				userDealRecord3.setUserId(phoneNumber);
				userDealRecord3.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
			}
			userDealRecord3.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
			
			
			userDealRecordMapper.insertSelective(userDealRecord1);
			if(originalPrice>currentPrice){
				userDealRecordMapper.insertSelective(userDealRecord2);
			}
			userDealRecordMapper.insertSelective(userDealRecord3);
		}
		
		if (company != null) {
			companyMapper.updateByPrimaryKeySelective(company);
			
		}
	}
	public void addAndUpdateInvoicePurchase(Invoice invoice,PurchaseOrder purchaseOrder){
		if(invoice!=null){
			invoiceMapper.insertSelective(invoice);
		}
		if(purchaseOrder!=null){
			purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		}
	}
	public void UpdateInvoicePurchase(Invoice invoice,PurchaseOrder purchaseOrder){
		if(invoice!=null){
			invoiceMapper.updateByPrimaryKeySelective(invoice);
		}
		if(purchaseOrder!=null){
			purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		}
	}
	public void updateUserAfterBuyVip(PurchaseOrder purchaseOrder, User user, UserDealRecord userDealRecord, UserDealRecord userDealRecord1, UserDealRecord userDealRecord2, UserDealRecord userDealRecord3) {
		purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		logger.warn("VIP个人 修改流水状态:"+JsonUtil.toJson(purchaseOrder));
		//userMapper.updateByPrimaryKeySelective(user);
		userExMapper.updateUserAdvisoryNum(user);
		logger.warn("VIP个人 增加用户VIP余额:"+JsonUtil.toJson(user));
		userDealRecordMapper.insert(userDealRecord);
		logger.warn("VIP个人 支付流水:"+JsonUtil.toJson(userDealRecord));
		userDealRecordMapper.insert(userDealRecord3);
		logger.warn("VIP公司入账流水:"+JsonUtil.toJson(userDealRecord3));
		userDealRecordMapper.insert(userDealRecord1);
		logger.warn("VIP个人 会员卡入账流水:"+JsonUtil.toJson(userDealRecord1));
		userDealRecordMapper.insert(userDealRecord2);
		logger.warn("VIP个人 公司支出流水:"+JsonUtil.toJson(userDealRecord2));
	}

	public void updateCompanyAfterBuyVip(PurchaseOrder purchaseOrder,
			Company company, UserDealRecord userDealRecord, UserDealRecord userDealRecord1, UserDealRecord userDealRecord2, UserDealRecord userDealRecord3) {
		purchaseOrderMapper.updateByPrimaryKeySelective(purchaseOrder);
		logger.warn("VIP公司  修改流水状态:"+JsonUtil.toJson(purchaseOrder));
		companyMapper.updateByPrimaryKeySelective(company);
		logger.warn("VIP公司  增加公司VIP余额:"+JsonUtil.toJson(company));
		userDealRecordMapper.insert(userDealRecord);
		logger.warn("VIP公司  支付流水:"+JsonUtil.toJson(userDealRecord));
		userDealRecordMapper.insert(userDealRecord3);
		logger.warn("VIP公司入账流水:"+JsonUtil.toJson(userDealRecord3));
		userDealRecordMapper.insert(userDealRecord1);
		logger.warn("VIP公司  会员卡入账:"+JsonUtil.toJson(userDealRecord1));
		userDealRecordMapper.insert(userDealRecord2);
		logger.warn("VIP公司  公司支出流水:"+JsonUtil.toJson(userDealRecord2));
	}

}
